package com.yq.backend.service.impl;

import cn.hutool.core.util.IdUtil;
import com.wf.captcha.base.Captcha;
import com.yq.backend.config.LoginCodeEnum;
import com.yq.backend.config.LoginProperties;
import com.yq.backend.config.RedisConfig;
import com.yq.backend.dto.SysLoginDto;
import com.yq.backend.pojo.Result;
import com.yq.backend.service.SysLoginService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @descriptions: 登录、退出业务实现类
 * @author: yunqiang
 * @date: 2022/3/28 14:06
 */
@Service
public class SysLoginServiceImpl implements SysLoginService {

    private static final Logger logger = LoggerFactory.getLogger(SysLoginServiceImpl.class);


    @Resource
    private RedisConfig redisConfig;

    @Resource
    private LoginProperties loginProperties;

    /**
     * 用户登录
     *
     * @param sysLoginDto
     * @return
     */
    @Override
    @Transactional
    public Result doLogin(SysLoginDto sysLoginDto) {
        if(StringUtils.isEmpty(sysLoginDto.getUsername()) || StringUtils.isEmpty(sysLoginDto.getPassword())){
            return Result.error(501,"用户名或密码为空");
        }
        if(StringUtils.isEmpty(sysLoginDto.getUuid()) || StringUtils.isEmpty(sysLoginDto.getCaptcha())){
            return Result.error(501,"验证码为空");
        }
        String captcha = String.valueOf(redisConfig.getRedisTemplateByDb(1).opsForValue().get(sysLoginDto.getUuid()));
        if (StringUtils.isBlank(captcha)) {
            return Result.error(501, "验证码错误");
        }
        if (StringUtils.isBlank(sysLoginDto.getCaptcha()) || !sysLoginDto.getCaptcha().equalsIgnoreCase(captcha)) {
            return Result.error(501, "验证码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("userName","admin");
        return Result.success(map);
    }

    /**
     * 用户退出
     *
     * @return
     */
    @Override
    public Result doLogout() {
        //获取用户
        return Result.success();
    }

    /**
     * 获取验证码
     * @return
     */
    @Override
    public Result getCode() {
        // 获取运算的结果
        Captcha captcha = loginProperties.getCaptcha();
        String uuid = IdUtil.simpleUUID();
        //当验证码类型为 arithmetic时且长度 >= 2 时，captcha.text()的结果有几率为浮点型
        String captchaValue = captcha.text();
        if (captcha.getCharType() - 1 == LoginCodeEnum.arithmetic.ordinal() && captchaValue.contains(".")) {
            captchaValue = captchaValue.split("\\.")[0];
        }
        // 保存
        redisConfig.getRedisTemplateByDb(1).opsForValue().set(uuid, captchaValue,loginProperties.getLoginCode().getExpiration(), TimeUnit.MINUTES);
        // 验证码信息
        Map<String, Object> imgResult = new HashMap<String, Object>(2) {{
            put("img", captcha.toBase64());
            put("uuid", uuid);
        }};
        return Result.success(imgResult);
    }
}
